<template>
  <div class="change">
    <div class="head">
      <h3>修改密码</h3>
    </div>
    <div class="main">
      <div class="item">
        <div style="margin-right: 10px">旧密码</div>
        <div>
          <el-input v-model="oldpwd" @blur="checkopwd" show-password></el-input>
        </div>
      </div>
      <div class="item">
        <div style="margin-right: 10px">新密码</div>
        <div><el-input v-model="newpwd" show-password></el-input></div>
      </div>
      <div class="item">
        <div style="margin-right: 10px">确认新密码</div>
        <div>
          <el-input v-model="again" show-password @focus="againpwd"></el-input>
        </div>
      </div>
      <div class="item">
        <el-button type="primary" @click="changepwd">确认</el-button>
        <el-button>重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { $_checkOpwd, $_changePwd } from "@/apis/user";
export default {
  data() {
    return {
      oldpwd: "",
      newpwd: "",
      again: "",
    };
  },
  methods: {
    checkopwd() {
      $_checkOpwd({
        oldPwd: this.oldpwd,
        id: localStorage.id,
      }).then((res) => {
        if (res.data.code == 1) {
          this.$message.error(res.data.msg);
        }
      });
    },
    againpwd() {
      if (this.again !== this.newpwd) {
        this.$message("请保证两次输入的密码相同");
      }
    },
    changepwd() {
      $_changePwd({
        newPwd: this.newpwd,
        id: localStorage.id,
      }).then((res) => {
        if (res.data.code == 0) {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
  },
};
</script>

<style lang='less' scoped >
.change {
  width: 98%;
  height: 100%;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 0 20px #212121;
  .head {
    height: 50px;
    padding: 0 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid #bdbdbd;
  }
  .main {
    padding: 20px;
    .item {
      margin: 20px;
      display: flex;
      align-items: center;
    }
  }
}
</style>